const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const TerserJSPlugin=require('terser-webpack-plugin')
const path = require('path');
const config = {
    mode: 'production',
    entry: './src/index.js',
    output: {
        filename: 'bundle.js',
        path: path.join(__dirname, './dist'),
        clean: true
    },
    module: {
        rules: [
            {
                test: /\.(scss|sass)$/,
                use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader']
            },
            {
                test: /\.js$/,
                loader: "babel-loader"
            }
        ]
    },
    optimization: {
        minimize: true,  //设置开发环境可用，若不设置默认false只能支持生产环境
        minimizer: [
            `...`,
            new CssMinimizerPlugin(),
            new TerserJSPlugin()
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({filename: "index.html", template: "template.html"}),
        new CopyWebpackPlugin(
            {
                patterns: [
                    {
                        from: path.join(__dirname, 'assets'),
                        to: 'assets'
                    }
                ]
            }
        ),
        new MiniCssExtractPlugin({
            filename: '[name].css',
            chunkFilename:'[id].css',
        }),
        new MiniCssExtractPlugin(),
        new TerserJSPlugin()
    ],

}
module.exports = config;